package initialize

import (
	"context"
	"fmt"
	"ksirGo/global"

	"github.com/go-redis/redis/v8"
	"go.uber.org/zap"
)

func Redis() *redis.Client {
	redisConfig := global.CONFIG.Redis
	if !redisConfig.Enable {
		return nil
	}
	rdb := redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", redisConfig.Host, redisConfig.Port),
		Password: redisConfig.Password,
		DB:       redisConfig.DB,
	})
	if pong, err := rdb.Ping(context.Background()).Result(); err != nil {
		global.LOGGER.Error("redis connect ping failed, err:", zap.Error(err))
		return nil
	} else {
		fmt.Println("redis连接成功! pong:", pong)
		return rdb
	}
}
